import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ProjectService } from 'src/app/core/service/project.service';
import { ApplicationService } from 'src/app/core/service/application.service';



@Component({
  selector: 'app-project-detail',
  templateUrl: './project-detail.component.html',
  styleUrls: ['./project-detail.component.scss']
})
export class ProjectDetailComponent implements OnInit {
  projectUuid = '';
  title = '';

  unauthorizedDataSet: any;

  highRiskDatas: any;

  appOverviewName = [];
  appOverviewDatas = [];

  appOverview = {
    color: ['#CB4649', '#F56D71', '#FDD691', '#258C75'],
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      x: 'left',
      // data: this.appOverviewName,
      data: ['严重', '高危', '中危', '轻危'],
      left: '20',
      textStyle: {
        color: '#fff'
      }
    },
    series: [
      {
        name: '应用概况',
        type: 'pie',
        radius: ['50%', '70%'],
        avoidLabelOverlap: false,
        label: {
          normal: {
            show: false,
            position: 'center'
          },
          emphasis: {
            show: true,
            textStyle: {
              fontSize: '30',
              fontWeight: 'bold'
            }
          }
        },
        labelLine: {
          normal: {
            show: true
          }
        },
        data: this.appOverviewDatas
      }
    ]
  };

  jarGradeName = [];
  jarGradeData = [];

  jarGrade = {

    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      x: 'left',
      left: '20',
      data: this.jarGradeName,
      textStyle: {
        color: '#fff'
      }
    },
    color: ['#3398DB', '#9999ff', '#F56D71', '#A93A40', '#4ddbff', '#FFCD5C', '#258CF5'],
    calculable: true,
    series: [
      {
        name: '半径模式',
        type: 'pie',
        radius: '65%',
        center: ['55%', '50%'],
        roseType: 'radius',
        label: {
          normal: {
            show: true,
            position: 'inner'
          },
          emphasis: {
            show: true
          }
        },
        lableLine: {
          normal: {
            show: true
          },
          emphasis: {
            show: true
          }
        },
        data: this.jarGradeData.sort(function (a, b) { return a.value - b.value; }),
      }
    ]
  };


  // cve 分值
  cveName = [];
  cveValue = [];

  // CVE分值
  cveCount = {
    color: ['#3398DB'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {            // 坐标轴指示器，坐标轴触发有效
        type: 'shadow'          // 默认为直线，可选为：'line' | 'shadow'
      },
      formatter: '数量: {c}  <br/> {a}: {b}'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        data: this.cveName,
        axisTick: {
          alignWithLabel: true
        },
        nameTextStyle: {
          color: '#fff'
        },
        axisLine: {
          lineStyle: {
            color: '#fff'
          }
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        nameTextStyle: {
          color: '#fff'
        },
        axisLine: {
          lineStyle: {
            color: '#fff'
          }
        }
      }
    ],
    series: [
      {
        name: 'CVE分值',
        type: 'bar',
        barWidth: '60%',
        // data: [10, 52, 200, 334, 390, 330, 220]
        data: this.cveValue
      }
    ]
  };

  // 许可使用情况
  permissionName = [];
  permissionDatas = [];
  permission = {
    color: ['#9999ff', '#F56D71', '#A93A40', '#4ddbff', '#FFCD5C', '#258CF5'],
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 'left',
      data: this.permissionName,
      textStyle: {
        color: '#fff'
      }
    },
    series: [
      {
        name: '许可使用情况',
        type: 'pie',
        radius: '55%',
        center: ['50%', '60%'],
        data: this.permissionDatas,
        itemStyle: {
          emphasis: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  };

  constructor(
    private projectService: ProjectService,
    private applicationService: ApplicationService,
    private routeInfo: ActivatedRoute
  ) { }


  getApplicationDetailOverview(projectUuid: string) {
    this.applicationService.getApplicationDetailOverview(projectUuid)
      .subscribe((appData) => {
        console.log('=== janine.getApplicationview().appData ===>', appData);
        if (appData.status === 1) {
          // tslint:disable-next-line: forin
          for (const key in appData.data) {
            this.appOverviewName.push(appData.data[key].name);
            const appOverviewDataItem = { value: 0, name: '' };
            appOverviewDataItem.value = appData.data[key].value;
            appOverviewDataItem.name = appData.data[key].name;
            this.appOverviewDatas.push(appOverviewDataItem);
          }
          this.appOverview = Object.assign({}, this.appOverview);
        }
      });
  }


  getJarGrade(projectUuid: string) {
    this.projectService.getJarGrade(projectUuid)
      .subscribe((jarData) => {
        console.log('=== janine.getJarGrade().jarData ===>', jarData);
        if (jarData.status === 1) {
          // tslint:disable-next-line: forin
          for (const key in jarData.data) {
            this.jarGradeName.push(jarData.data[key].name);

            const jarDataItem = { value: 0, name: '' };
            jarDataItem.value = jarData.data[key].value;
            jarDataItem.name = jarData.data[key].name;
            this.jarGradeData.push(jarDataItem);
          }
          this.jarGrade = Object.assign({}, this.jarGrade);
        }
      });
  }


  getCveCount(projectUuid: string) {
    this.projectService.getCveCount(projectUuid)
      .subscribe((cveData) => {
        if (cveData.status === 1) {
          // tslint:disable-next-line: forin
          for (const key in cveData.data) {
            this.cveName.push(cveData.data[key].name);
            this.cveValue.push(cveData.data[key].value);
          }
          this.cveCount = Object.assign({}, this.cveCount);
        }
      });
  }

  getPermission(projectUuid: string) {
    this.projectService.getPermission(projectUuid)
      .subscribe((permissionData) => {
        if (permissionData.status === 1) {
          // tslint:disable-next-line: forin
          for (const key in permissionData.data) {
            this.permissionName.push(permissionData.data[key].name);
            const permissionDataItem = { value: 0, name: '' };
            permissionDataItem.value = permissionData.data[key].value;
            permissionDataItem.name = permissionData.data[key].name;
            this.permissionDatas.push(permissionDataItem);
          }
          this.permission = Object.assign({}, this.permission);
        }
      });
  }

  /**
   * 高危TOP10
   * @param reset 获取数据
   */
  getHighRisk(projectUuid: string): void {
    this.projectService.getHighRisk(projectUuid).subscribe((highRiskData) => {
      if (highRiskData.status === 1) {
        this.highRiskDatas = highRiskData.data;
      }
    });
  }

  /**
   * 未授权软件列表
   * @param reset 获取数据
   */
  getUnauthorizedPage(projectUuid: string): void {
    this.projectService.getUnauthorizedPage(projectUuid).subscribe((unauthorizedPage) => {
      if (unauthorizedPage.status === 1) {
        this.unauthorizedDataSet = unauthorizedPage.data;
      }
    });
  }

  ngOnInit() {
    this.projectUuid = this.routeInfo.snapshot.queryParams['projectUuid'];
    this.title = this.routeInfo.snapshot.queryParams['title'];
    console.log(`=== janine.project.detail.projectUuid ===${this.projectUuid} === ${this.title}`);
    this.getApplicationDetailOverview(this.projectUuid);
    this.getJarGrade(this.projectUuid);
    this.getCveCount(this.projectUuid);
    this.getPermission(this.projectUuid);
    this.getHighRisk(this.projectUuid);
    this.getUnauthorizedPage(this.projectUuid);
  }

}
